﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using shenghua.Commodity.Commodities;
using shenghua.Common.Comments;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Order.Orders
{
    public class OrderEvaluateManager: CommentManager<OrderEvaluate>
    {
        public OrderEvaluateManager(
            IRepository<OrderEvaluate> repository
            ): base(repository)
        {

        }

        public async Task<OrderEvaluate> MemberEvaluate(Member.Members.Member member, Order order, OrderEvaluate comment, OrderEvaluate parentComment = null)
        {
            if(parentComment != null && parentComment.OrderNumber != order.OrderNumber)
            {
                throw new UserFriendlyException("非法操作，无法进行评论");
            }
            comment.OrderNumber = order.OrderNumber;
            comment.TargetId = order.Id;
            await base.MemberEvaluate(member, comment, parentComment);
            return comment;
        }

        public virtual async Task<List<OrderEvaluate>> GetOrderComments(long? userId, string orderNumber)
        {
            var query = _repository.GetAll();
            if (userId != null) query = query.Where(r => r.UserId == userId);
            return query.Where(r => r.OrderNumber == orderNumber && r.ParentCommentId == null).ToList();
        }
    }
}
